<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        // 单向链表反转
        class Node {
            constructor(data) {
                this.data = data
                this.next = null
            }
        }
        class linklisted {
            constructor(data) {
                this.head = null
                this.length = 0
            }
            append(value) {
                let node = new Node(value)
                if (this.head == null) {
                    this.head = node
                    this.length++
                } else {
                    let current = this.head
                    while (current.next) {
                        current = current.next
                    }
                    current.next = node
                    this.length++
                }
            }
            reserve() {
                let current = this.head//为了不对头结点用影响，这里用变量current指向head的地址
                let pre = null//用于记入上一个元素元素
                let n = null//用于记入下一个元素
                while (current) {
                    n = current.next//获取头结点的下一个元素
                    current.next = pre//让头结点的下一个元素指向上一个元素,第一次为null
                    pre = current//保存当前元素
                    current = n//移动current 的指针
                }
                return pre
            }
        }
        let list = new linklisted()
        list.append(1)
        list.append(2)
        list.append(3)
        list.append(4)
        console.log(list.reserve())


    </script>
</body>

</html>